#include "OpMove.h"
#include "Qt3DPicker.h"
#include "INode.h"
#include "Registry.h"
#include "ActorTable.h"
#include "Ent.h"

class OpMovePrivate
{
public:
	std::set<std::shared_ptr<INode>> m_children;
	osg::Vec3d m_offset;
};

OpMove::OpMove(const std::set<std::shared_ptr<INode>>& children, const osg::Vec3d offset, QUndoCommand *parent)
	: Op(parent)
	,d_ptr(new OpMovePrivate)
{
	d_ptr->m_children = children;
	d_ptr->m_offset = offset;
}

OpMove::~OpMove()
{
	delete d_ptr;
}

#include "QtOSGWidget.h"
void OpMove::redo()
{
	Registry &reg = Registry::instance();
	auto widget3D = reg.get<QtOSGWidget>(widget3DID);
	widget3D->pauseRenderThread();

	for (auto p : d_ptr->m_children)
	{
		std::shared_ptr<Ent> ent = std::dynamic_pointer_cast<Ent>(p);
		ent->move(d_ptr->m_offset);
	}
	d_ptr->m_offset = -d_ptr->m_offset;
	widget3D->resumeRenderThread();
}

void OpMove::undo()
{
	redo();
}
